package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.google.android.flexbox.FlexboxLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.response.GetImportToolIndexBean;
import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.eventbus_event.UploadSuccessEvent;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.ImAbstractObjectBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.bean.goods.RequestBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.BarCodeEvent;
import com.wwwarehouse.resource_center.eventbus_event.CategoryEvent;
import com.wwwarehouse.resource_center.eventbus_event.ChooseUnitEvent;
import com.wwwarehouse.resource_center.eventbus_event.GoodsEvent;
import com.wwwarehouse.resource_center.eventbus_event.ImagePathEvent;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;
import com.wwwarehouse.resource_center.eventbus_event.StringEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;
import com.wwwarehouse.resource_center.utils.UploadPicUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.resource_center.constant.ResourceConstant.PATH_CREATEGOODS;

/**
 * Description:  创建商品
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/7 14:28
 */
@Route(path = PATH_CREATEGOODS)
public class CreateGoodsFragment extends TheParentFragment implements View.OnClickListener {

    public RelativeLayout mGoodsName;
    public RelativeLayout mGoodsPicture;
    public RelativeLayout mGoodsCategory;
    public LinearLayout mGoodsDescribe;
    public RelativeLayout mGoodsLabel;
    public RelativeLayout mGoodsAttribute;
    public RelativeLayout mGoodsTagBarCode;
    public RelativeLayout mRelUnit;
    public TextView mName;
    public TextView mCategory;
    public TextView mDes;
    public ImageView mImageAttribute;
    public TextView mTextAttribute;
    public TextView mTag;
    public TextView mBarCode;
    public TextView mSequenceCode;
    public LinearLayout mLLBar;
    public LinearLayout mLLLaber;
    public FlexboxLayout mLaber;
    public FlexboxLayout mAttr;
    public ImageView mIvPic;
    public LinearLayout mLLPic;
    public ElasticScrollView elasticScrollView;

    public boolean isShowFinish = true;
    public boolean isShowToImport = true;
    public AutoClickButton mToPCImport;
    public String path;
    public ArrayList<GoodsSelectLabelBean.ListBean> data;
    public ArrayList<GoodsAttributesBean.ListBean> listBeen;
    public String categoryUkid;
    public String ownerUkid;
    public File mFile;
    public FileUploadBean fileUploadBean;
    public String operationUkid;
    public TextView img_descrip;
    public String goodsName;
    public String goodsDes;
    public ImageView no_pic;
    public ImageView category_item;
    public ImageView img_name;
    public String metaCategoryUkid;
    public TextView mUnit;
    public String unitUkid;
    public String abstractObjectUkid;
    public boolean isToPcImport;
    public int pos;  //当前第几页
    protected String mUnitCharacter = "";

    @Override
    protected int getContentId() {
        return R.layout.fragment_create_goods_c;
    }

    @Override
    protected void init(View view) {
        mGoodsName = (RelativeLayout) view.findViewById(R.id.rel_name);
        mGoodsPicture = (RelativeLayout) view.findViewById(R.id.rel_picture);
        mRelUnit = (RelativeLayout) view.findViewById(R.id.rel_unit);
        mGoodsCategory = (RelativeLayout) view.findViewById(R.id.rel_category);
        mGoodsDescribe = (LinearLayout) view.findViewById(R.id.rel_describe);
        mGoodsLabel = (RelativeLayout) view.findViewById(R.id.rel_label);
        mGoodsTagBarCode = findView(view, R.id.rel_tag);
        mGoodsAttribute = (RelativeLayout) view.findViewById(R.id.rel_attribute);
        mLLBar = (LinearLayout) view.findViewById(R.id.ll_bar);
        mLLLaber = (LinearLayout) view.findViewById(R.id.ll_laber);
        mLaber = (FlexboxLayout) view.findViewById(R.id.fl_laber);
        mAttr = (FlexboxLayout) view.findViewById(R.id.fl_attr);
        category_item = findView(view, R.id.category_item);
        elasticScrollView = (ElasticScrollView) view.findViewById(R.id.goods_scroll);
        img_name = findView(view, R.id.img_name);
        mLLPic = (LinearLayout) view.findViewById(R.id.ll_pic);
        mIvPic = (ImageView) view.findViewById(R.id.iv_pic);
        no_pic = findView(view, R.id.no_pic);
        mName = (TextView) view.findViewById(R.id.tv_name);
        mUnit = (TextView) view.findViewById(R.id.tv_unit);

        mDes = (TextView) view.findViewById(R.id.tv_des);
        mTag = (TextView) view.findViewById(R.id.tv_tag);
        mBarCode = (TextView) view.findViewById(R.id.tv_barcode);
        mSequenceCode = (TextView) view.findViewById(R.id.tv_sequencecode);
        mCategory = (TextView) view.findViewById(R.id.tv_category);
        mLLBar.setVisibility(View.GONE);

        mLLLaber.setVisibility(View.VISIBLE);
        img_descrip = findView(view, R.id.img_descrip);
        mImageAttribute = (ImageView) view.findViewById(R.id.iv_att);
        mTextAttribute = (TextView) view.findViewById(R.id.tv_att);

        mToPCImport = (AutoClickButton) view.findViewById(R.id.import_to_pc_btn);
        mToPCImport.setTextColor(Color.parseColor("#FFFFFF"));
        mGoodsAttribute.setSelected(false);
        mImageAttribute.setSelected(false);
        mTextAttribute.setSelected(false);
        mGoodsPicture.setSelected(false);
        mGoodsAttribute.setOnClickListener(null);


        EventBus.getDefault().register(this);
        mGoodsName.setOnClickListener(this);
        mGoodsPicture.setOnClickListener(this);
        mGoodsCategory.setOnClickListener(this);
        mGoodsDescribe.setOnClickListener(this);
        mGoodsTagBarCode.setOnClickListener(this);
        mGoodsLabel.setOnClickListener(this);
        if (mRelUnit!=null){
            mRelUnit.setOnClickListener(this);
        }
        Bundle bundle = getArguments();
        try {
            CardDeskTaskResponseBean.TaskBean taskBean = (CardDeskTaskResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS);
            if (null != taskBean) {
                this.ownerUkid = taskBean.getBelongBusiness();
                this.operationUkid = taskBean.getCardUkid();
            } else {
                CardDeskFunctionResponseBean.TaskBean mBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
                if (null != mBean) {
//                        this.operationUkid = mBean.getTaskTypeUkid();
                    this.ownerUkid = mBean.getBusinessId();
                }
            }
        } catch (Exception e) {

        }
        elasticScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
            @Override
            public void onDownPull() {
                mToPCImport.setVisibility(View.GONE);
                if (isShowFinish) {
                    ((BaseCardDeskActivity) mActivity).showTitleBt1();
                    ((BaseCardDeskActivity) mActivity).setTitleBt1Text(R.string.complete);
                    boolean verification = verification();
                    if (verification) {
                        ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);
                    } else {
                        ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(false);
                    }

                } else {
                    ((BaseCardDeskActivity) mActivity).hideTitleBt();
                }
                isShowFinish = !isShowFinish;
            }

            @Override
            public void onUpPull() {
                ((BaseCardDeskActivity) mActivity).hideTitleBt();
                if (isShowToImport) {
                    mToPCImport.setVisibility(View.VISIBLE);
                    mToPCImport.setEnabled(true);
                    mToPCImport.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if (!TextUtils.isEmpty(categoryUkid)) {
                                Map<String, Object> map = new HashMap<>();
                                map.put("businessUnitId", ownerUkid);
                                map.put("templateType", "商品");
                                map.put("categoryUkid", categoryUkid);
                                if (operationUkid != null && !operationUkid.isEmpty()) {
                                    map.put("previousOperationUkid", operationUkid); //任务UKID
                                }
                                httpPost(ResourceConstant.GOTO_IMPORT_TOOL_INDEX, map, 1, true, "");
                            } else {
                                isToPcImport = true;

                                Bundle b = new Bundle();
                                b.putString("ownerUkid", ownerUkid);
                                ChooseCategoryFagment chooseCategoryFagment = new ChooseCategoryFagment();
                                chooseCategoryFagment.setArguments(b);
                                pushFragment(chooseCategoryFagment);
                            }


                        }
                    });
                } else {
                    mToPCImport.setVisibility(View.GONE);
                }
                isShowToImport = !isShowToImport;
            }
        });
    }

    @Override
    public void titleBt1Click() {
        if (peekFragment() instanceof CreateGoodsFragment) {
            if (fileUploadBean!=null){
                request(ResourceConstant.SAVE_OBJECT, "");
                return;
            }
            if (!TextUtils.isEmpty(path)) {
                showProgressDialog("图片上传中...");
                UploadPicUtil.getInstance().uploadPic(path, mFile, getActivity());
            } else {
                request(ResourceConstant.SAVE_OBJECT, "");
            }
        }
    }

    //保存信息
    public void request(String url, String cardType) {
//        try {
        Map<String, Object> map = new HashMap<>();

        //标签列表
        List<RequestBean> labelList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            RequestBean labelBean = new RequestBean();
            labelBean.setLabelUkid(data.get(i).getLabelUkid() + "");
            labelList.add(labelBean);
        }
        //条码列表    type:条码为1；69码为4
        List<RequestBean> identifyList = new ArrayList<>();
        RequestBean barCodeBean = new RequestBean();
        barCodeBean.setIdentifyCode(mBarCode.getText().toString().trim());
        barCodeBean.setIdentifyType("0");
        identifyList.add(barCodeBean);
        if (mSequenceCode.getText().length() > 0) {
            RequestBean sequenceCodeBean = new RequestBean();
            sequenceCodeBean.setIdentifyCode(mSequenceCode.getText().toString().trim());
            sequenceCodeBean.setIdentifyType("4");
            identifyList.add(sequenceCodeBean);
        }
        //分类列表
        List<RequestBean> categoryList = new ArrayList<>();
        RequestBean categoryCodeBean = new RequestBean();
        categoryCodeBean.setCategoryUkid(categoryUkid);
        categoryList.add(categoryCodeBean);

        //属性列表
        List<RequestBean> attributeList = new ArrayList<>();

        for (int i = 0; i < listBeen.size(); i++) {
            RequestBean attributeBean = new RequestBean();
            attributeBean.setAttributeUkid(listBeen.get(i).getAttributeUkid() + "");
            attributeBean.setAttributeValue(listBeen.get(i).getAttributeValue());
            attributeList.add(attributeBean);
        }

        ImAbstractObjectBean imAbstractObjectBean = new ImAbstractObjectBean();
        //媒体列表
        if (fileUploadBean != null) {
            List<RequestBean> mediaList = new ArrayList<>();
            RequestBean mediaBean = new RequestBean();
            mediaBean.setMediaName(fileUploadBean.getData().get(0).getName());
            mediaBean.setMediaType("0");
            mediaBean.setRelatedUrl(fileUploadBean.getData().get(0).getPath());
            mediaList.add(mediaBean);
            imAbstractObjectBean.setMediaList(mediaList); //媒体列表

        }
        if (!TextUtils.isEmpty(abstractObjectUkid)) {
            imAbstractObjectBean.setAbstractObjectUkid(abstractObjectUkid);
        }
        imAbstractObjectBean.setType("CREATE_H_GOODS"); //1-商品、2-工具、3-店铺、4-仓库
        imAbstractObjectBean.setName(mName.getText().toString().trim());  //物的名称
        imAbstractObjectBean.setLabelList(labelList); //标签列表
        imAbstractObjectBean.setIntroduction(mDes.getText().toString().trim()); //描述
        imAbstractObjectBean.setIdentifyList(identifyList); //标示码列表
        imAbstractObjectBean.setOwnerUkid(ownerUkid);   //业务单位UKid
        imAbstractObjectBean.setCategoryList(categoryList); //分类列表
        imAbstractObjectBean.setAttributeList(attributeList); //属性列表
        if (unitUkid != null && !unitUkid.isEmpty()) {
            imAbstractObjectBean.setUnitUkid(unitUkid); //单位
        }
        if (operationUkid != null && !operationUkid.isEmpty()) {
            map.put("operationUkid", operationUkid); //任务UKID
        }
        map.put("imAbstractObject", imAbstractObjectBean);
        if (!TextUtils.isEmpty(cardType)) {
            map.put("cardType", cardType);
        }
        String json = JSON.toJSONString(map);
        LogUtils.showLog(json);
        httpPost(url, map, 0, true, "");
//        } catch (Exception e) {
//            LogUtils.showLog("商品信息保存失败!");
//        }


    }

    public boolean verification() {
        boolean b;
        try {
            //验证保存信息
            String name = mName.getText().toString(); //名称
            String category = mCategory.getText().toString();//分类
            String des = mDes.getText().toString(); //描述
            String barCode = mBarCode.getText().toString();//条码标示信息
            b =!TextUtils.isEmpty(unitUkid)&& !TextUtils.isEmpty(name)&& !TextUtils.isEmpty(category) && !TextUtils.isEmpty(des) &&!TextUtils.isEmpty(barCode)&& data != null && !data.isEmpty() && listBeen != null && !listBeen.isEmpty();
            if (getString(R.string.goods_name).equals(name)) {
                b = false;
            }
            if (getString(R.string.goods_describe).equals(des)){
                b = false;
            }

        } catch (Exception e) {
            b = false;
        }
        return b;
    }

    public void onEventMainThread(Object event) {
        if (event instanceof StringEvent && "create_name".equals(((StringEvent) event).getFrom())) {
            //名称
            goodsName = ((StringEvent) event).getMsg();
            mName.setText(((StringEvent) event).getMsg());
            popFragment();
        } else if (event instanceof CategoryEvent && "finish".equals(((CategoryEvent) event).getForm())) {
            //分类
            for (int i = 0; i < Integer.parseInt(((CategoryEvent) event).getMsg().getLevel()); i++) {
                popFragment();
            }
            if (isToPcImport) {
                Map<String, Object> map = new HashMap<>();
                map.put("businessUnitId", ownerUkid);
                map.put("templateType", "商品");
                map.put("categoryUkid", ((CategoryEvent) event).getMsg().getCategoryUkid());
                if (operationUkid != null && !operationUkid.isEmpty()) {
                    map.put("previousOperationUkid", operationUkid); //任务UKID
                }
                httpPost(ResourceConstant.GOTO_IMPORT_TOOL_INDEX, map, 1, true, "");
            } else {
                mCategory.setText(((CategoryEvent) event).getMsg().getName());
                categoryUkid = ((CategoryEvent) event).getMsg().getCategoryUkid();
                metaCategoryUkid = ((CategoryEvent) event).getMsg().getMetaCategoryUkid();
                mTextAttribute.setVisibility(View.VISIBLE);
                listBeen = null;
                mGoodsAttribute.setSelected(true);
                mImageAttribute.setSelected(true);
                mTextAttribute.setSelected(true);
                mGoodsAttribute.setOnClickListener(this);
                if (mAttr.getChildCount() != 0) {
                    mAttr.removeAllViews();
                }
            }

        } else if (event instanceof ImagePathEvent) {
            //显示图片
            path = ((ImagePathEvent) event).getMsg();
            mFile = ((ImagePathEvent) event).getmFile();
            mLLPic.setVisibility(View.GONE);
            mIvPic.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage("file://" + ((ImagePathEvent) event).getMsg(), mIvPic);
            mGoodsPicture.setSelected(true);

        } else if (event instanceof StringEvent && "create_des".equals(((StringEvent) event).getFrom())) {
            //描述
            goodsDes = ((StringEvent) event).getMsg();
            mDes.setText(goodsDes);
            popFragment();
        } else if (event instanceof BarCodeEvent) {
            //标示信息
            mTag.setVisibility(View.GONE);
            mLLBar.setVisibility(View.VISIBLE);
            mBarCode.setText(((BarCodeEvent) event).getmBarCode());
            mSequenceCode.setText(((BarCodeEvent) event).getmSequenceCode());
            popFragment();

        } else if (event instanceof GoodsEvent && ((GoodsEvent) event).getCode() == GoodsEvent.GOODS_LABEL_SELECT_NOTICE) {
            //标签
            try {
                data = new ArrayList<>();
                ArrayList<GoodsSelectLabelBean.ListBean> dataList = (ArrayList<GoodsSelectLabelBean.ListBean>) ((GoodsEvent) event).getData();
                data.addAll(dataList);
                setLaber(data);


            } catch (Exception e) {

            }


        } else if (event instanceof RulesEvent && GoodsEvent.GOODS_ATTRIBUTE_SELECT_NOTICE == ((RulesEvent) event).getCode()) {
            try {
                listBeen = (ArrayList<GoodsAttributesBean.ListBean>) ((RulesEvent) event).getData();
                setAttributes(listBeen);


            } catch (Exception e) {

            }


        } else if (event instanceof UploadSuccessEvent) {
            dismissProgressDialog();
            fileUploadBean = ((UploadSuccessEvent) event).getFileUploadBean();
            if (!TextUtils.isEmpty(abstractObjectUkid)) {
                request(ResourceConstant.UPDATE_ABSTRACT_OBJECT, "UPDATE_H_GOODS");
            } else {
                request(ResourceConstant.SAVE_OBJECT, "");
            }
        } else if (event instanceof BackListenerEvent) {

            if ("CreateGoodsFragment".equals(((BackListenerEvent) event).getMsg())) {
                DialogTools.getInstance().showTCDialog(getContext(), "提示", "返回将不会保存当前编辑内容，确定返回吗？", "再看看", "返回", null, new DialogTools.DismissListener() {
                    @Override
                    public void setDismissListener(Dialog dialog, View view) {
                        dialog.dismiss();
                        popFragment();
                    }
                });
            }
        } else if (event instanceof ChooseUnitEvent) {
            popFragment();
            if (!TextUtils.isEmpty(((ChooseUnitEvent) event).getMsg().getUnitCharacter())) {
                mUnitCharacter = "（" + ((ChooseUnitEvent) event).getMsg().getUnitCharacter() + "）";
            }
            if (!TextUtils.isEmpty(((ChooseUnitEvent) event).getMsg().getUnitCharacter())) {
                mUnit.setText(((ChooseUnitEvent) event).getMsg().getUnitName() + "（" + ((ChooseUnitEvent) event).getMsg().getUnitCharacter() + "）");
            } else {
                mUnit.setText(((ChooseUnitEvent) event).getMsg().getUnitName());
            }

            unitUkid = ((ChooseUnitEvent) event).getMsg().getUnitUkid();
        }

    }

    public void setAttributes(ArrayList<GoodsAttributesBean.ListBean> listBeen) {
        mAttr.removeAllViews();
        mTextAttribute.setVisibility(View.GONE);
        if (listBeen.size() >= 4) {
            for (int i = 0; i < 4; i++) {
                TextView textView = new TextView(getContext());
//                        textView.setWidth(ConvertUtils.dip2px(getContext(), 100));
                textView.setWidth(ConvertUtils.dip2px(getContext(), 130));
                textView.setTextColor(Color.parseColor("#FFFFFF"));
                textView.setTextSize(12);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 10), ConvertUtils.dip2px(getContext(), 10), 0, 0);
                textView.setLayoutParams(layoutParams);
                textView.setText(listBeen.get(i).getAttributeName() + ": " + listBeen.get(i).getAttributeValue());
                mAttr.addView(textView);
            }
        } else {
            for (int i = 0; i < listBeen.size(); i++) {
                TextView textView = new TextView(getContext());
                textView.setWidth(ConvertUtils.dip2px(getContext(), 130));
//                        textView.setHeight(ConvertUtils.dip2px(getContext(), 100));
                textView.setTextColor(Color.parseColor("#FFFFFF"));
                textView.setTextSize(12);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 10), ConvertUtils.dip2px(getContext(), 8), 0, 0);
                textView.setLayoutParams(layoutParams);
                textView.setText(listBeen.get(i).getAttributeName() + ": " + listBeen.get(i).getAttributeValue());


                mAttr.addView(textView);

            }
        }
    }

    public void setLaber(ArrayList<GoodsSelectLabelBean.ListBean> data) {
        mLLLaber.setVisibility(View.GONE);
        mLaber.removeAllViews();
        if (data.size() < 3) {
            for (int i = 0; i < data.size(); i++) {
                TextView textView = getTextView();

                textView.setTextColor(Color.parseColor("#337CFF"));
                textView.setText(data.get(i).getLabelName());
                textView.setBackgroundResource(R.drawable.laber_bg_selector);
                textView.setSelected(true);

                mLaber.addView(textView);
            }
            for (int i = 0; i < 3 - data.size(); i++) {
                TextView textView = getTextView();
                textView.setBackgroundResource(R.drawable.laber_bg_selector);
                textView.setSelected(false);
                mLaber.addView(textView);
            }
        } else if (data.size() >= 3) {
            for (int i = 0; i <= 2; i++) {
                TextView textView = new TextView(getContext());
                textView.setWidth(ConvertUtils.dip2px(getContext(), 100));
                textView.setHeight(ConvertUtils.dip2px(getContext(), 27));
                textView.setBackgroundResource(R.drawable.laber_bg_selector);
                textView.setTextColor(Color.parseColor("#337CFF"));
                textView.setTextSize(11);
                textView.setGravity(Gravity.CENTER);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                if (i == 2) {
                    textView.setText(R.string.ellipsis);
                } else {
                    textView.setText(data.get(i).getLabelName());
                }
                textView.setSelected(true);
                FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 6), ConvertUtils.dip2px(getContext(), 8), 0, 0);
                textView.setLayoutParams(layoutParams);
                mLaber.addView(textView);
            }

        }
    }

    @NonNull
    public TextView getTextView() {
        TextView textView = new TextView(getContext());
        textView.setWidth(ConvertUtils.dip2px(getContext(), 100));
        textView.setHeight(ConvertUtils.dip2px(getContext(), 27));
        textView.setBackgroundResource(R.drawable.laber_bg_selector);
        textView.setMaxLines(1);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(12);
        FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 6), ConvertUtils.dip2px(getContext(), 8), 0, 0);
        textView.setLayoutParams(layoutParams);
        return textView;
    }

    @Override
    public void onClick(View v) {
        Bundle b = new Bundle();
        b.putString("ownerUkid", ownerUkid);
        int i = v.getId();
        initState();
        if (i == R.id.rel_name) {
            //名称
            GoodsAddNameFragment goodsAddNameFragment = new GoodsAddNameFragment();
            Bundle name = new Bundle();
            name.putString("name", goodsName);
            goodsAddNameFragment.setArguments(name);
            pushFragment(goodsAddNameFragment);
        } else if (i == R.id.rel_picture) {

            UploadPicFragment uploadPicFragment = new UploadPicFragment();
            Bundle bundle = new Bundle();
            if (null != fileUploadBean) {
                try {
                    bundle.putString("path", fileUploadBean.getData().get(0).getPath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                bundle.putString("path", path);
            }
            uploadPicFragment.setArguments(bundle);
            pushFragment(uploadPicFragment);

        } else if (i == R.id.rel_category) {
            //类目
            isToPcImport = false;
            ChooseCategoryFagment chooseCategoryFagment = new ChooseCategoryFagment();
            chooseCategoryFagment.setArguments(b);
            pushFragment(chooseCategoryFagment);
        } else if (i == R.id.rel_describe) {
            //描述
            GoodsDescripFragment goodsDescripFragment = new GoodsDescripFragment();

            b.putString("des", goodsDes);
            goodsDescripFragment.setArguments(b);
            pushFragment(goodsDescripFragment);
        } else if (i == R.id.rel_label) {
            //标签
            sp.putValue("businessId", ownerUkid);
            GoodsSelectTheLabelParentFragment fragment = new GoodsSelectTheLabelParentFragment();
            b.putParcelableArrayList("LabelList", data);
            fragment.setArguments(b);
            pushFragment(fragment);
        } else if (i == R.id.rel_tag) {
            // 添加标识标签
            GoodsAddTheTagBarcodeFragment goodsAddTheTagBarcodeFragment = new GoodsAddTheTagBarcodeFragment();
            b.putString("barcode", mBarCode.getText().toString().trim());
            b.putString("sequencecode", mSequenceCode.getText().toString().trim());
            goodsAddTheTagBarcodeFragment.setArguments(b);
            pushFragment(goodsAddTheTagBarcodeFragment);

        } else if (i == R.id.rel_attribute) {
            //属性
            GoodsAttributesProtectFragment goodsAttributesSelectParentFragment = new GoodsAttributesProtectFragment();
            Bundle bundle = new Bundle();
            bundle.putString("categoryUkid", metaCategoryUkid);
            bundle.putString("businessId", ownerUkid);
            bundle.putParcelableArrayList("AttributeList", listBeen);
            goodsAttributesSelectParentFragment.setArguments(bundle);
            pushFragment(goodsAttributesSelectParentFragment);
        } else if (i == R.id.rel_unit) {
            //单位
            ChooseUnitFragment chooseUnitFragment = new ChooseUnitFragment();
            Bundle bundle = new Bundle();
            bundle.putString("ownerUkid", ownerUkid);
            bundle.putString("unitUkid", unitUkid);
            chooseUnitFragment.setArguments(bundle);
            pushFragment(chooseUnitFragment);
        }
    }

    public void initState() {
        ((BaseCardDeskActivity) mActivity).hideTitleBt();
        mToPCImport.setVisibility(View.GONE);
        isShowFinish = true;
        isShowToImport = true;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof CreateGoodsFragment){
            mActivity.setTitle(R.string.create_goods);
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        try {

            if (what == 0) {
                if (TextUtils.equals("0", commonClass.getCode())) {
                    EventBus.getDefault().post(new GoodsEvent(GoodsEvent.GOODS_MODIFY, pos));
                    EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
                } else {
                    toast(commonClass.getMsg());
                }
            } else if (what == 1) {

                if (TextUtils.equals("0", commonClass.getCode())) {
                    if (commonClass.getData() != null) {
                        GetImportToolIndexBean bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
                        if (bean != null) {
                            popFragment();
                            if (bean.getPageType() == 1) {
                                //跳转到生成短链接的页面
                                Bundle bundle = new Bundle();
                                bundle.putString(Constant.KEY_INTENT_IMPORT_TYPE, "IMPORT_GOODS");
                                bundle.putSerializable(Constant.KEY_INTENT_IMPORT_TOOL, bean);
                                pushFragment("/contract/ImportHaveSortFragment", bundle);

                            } else if (bean.getPageType() == 2) {
                                //跳转到时间页面
                                Bundle bundle = new Bundle();
                                bundle.putString(Constant.KEY_INTENT_IMPORT_TYPE, "IMPORT_GOODS");
                                bundle.putSerializable(Constant.KEY_INTENT_IMPORT_TIME, bean);
                                pushFragment("/contract/DurationTimeFragment", bundle);

                            }
                        }
                    }
                } else {
                    toast(commonClass.getMsg());
                }


            }
        } catch (Exception e) {

        }

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        initState();
        EventBus.getDefault().unregister(this);
    }

}
